
//
//  FIDFMDBManager.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/27.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "FIDFMDBManager.h"

NSString * const kRongGaoDataBaseName = @"RongGao";
NSString * const kRongGaoCameraTableName = @"RG_cameraTUTK_list";

@implementation FIDFMDBManager

+ (FMDatabase *)createSqliteWithName:(NSString *)name{
    
    NSString *datebaseName = [NSString stringWithFormat:@"%@.sqlite",name];
    FMDatabase *database = [FMDatabase databaseWithPath:DocumentPATHFile(datebaseName)];
    return database;
    
}

+ (BOOL)executeUpdateWithSqlstatement:(NSString *)sqlstatement{
    FMDatabase *database = [FIDFMDBManager createSqliteWithName:kRongGaoDataBaseName];
    
    BOOL update;
    if ([database open]) {
        
        update =  [database executeUpdate:sqlstatement];
        
    }
    
    [database close];
    
    return update;
    
}
+ (NSMutableArray *)executeQueryWithSqlstatementCustom:(NSString *)sqlstatement{
    FMDatabase *database = [FIDFMDBManager createSqliteWithName:kRongGaoDataBaseName];
    
    NSMutableArray *params = [NSMutableArray array];
    if ([database open]) {
        
        FMResultSet *resultSet = [database executeQuery:sqlstatement];
        int cnt = 0;

        while ([resultSet next] && cnt ++ < MAX_CAMERA_LIMIT) {
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
          
            [param setObject:[resultSet stringForColumn:@"uid"] forKey:@"uid"];
            [param setObject:[resultSet stringForColumn:@"channel"] forKey:@"channel"];
            [param setObject:[resultSet stringForColumn:@"dev_nickname"] forKey:@"dev_nickname"];
            [param setObject:[resultSet stringForColumn:@"dev_pwd"] forKey:@"dev_pwd"];
            [param setObject:[resultSet stringForColumn:@"view_acc"] forKey:@"view_acc"];
            [param setObject:[resultSet stringForColumn:@"dev_name"] forKey:@"dev_name"];
            MyCamera *camera = [[MyCamera alloc] initWithName:[resultSet stringForColumn:@"dev_nickname"] viewAccount:[resultSet stringForColumn:@"view_acc"] viewPassword:[resultSet stringForColumn:@"dev_pwd"]];
            [camera setLastChannel:[resultSet intForColumn:@"channel"]];
            [camera connect:[resultSet stringForColumn:@"uid"]];
            [camera start:0];
            
            
            
            SMsgAVIoctrlGetAudioOutFormatReq *s = (SMsgAVIoctrlGetAudioOutFormatReq *)malloc(sizeof(SMsgAVIoctrlGetAudioOutFormatReq));
            s->channel = 0;
            [camera sendIOCtrlToChannel:0 Type:IOTYPE_USER_IPCAM_GETAUDIOOUTFORMAT_REQ Data:(char *)s DataSize:sizeof(SMsgAVIoctrlGetAudioOutFormatReq)];
            free(s);
            
            SMsgAVIoctrlGetSupportStreamReq *s2 = (SMsgAVIoctrlGetSupportStreamReq *)malloc(sizeof(SMsgAVIoctrlGetSupportStreamReq));
            [camera sendIOCtrlToChannel:0 Type:IOTYPE_USER_IPCAM_GETSUPPORTSTREAM_REQ Data:(char *)s2 DataSize:sizeof(SMsgAVIoctrlGetSupportStreamReq)];
            free(s2);
            
            
            [params addObject:camera];
            
            
        }
        
    }
    [database close];
    
    return params ;

}
+ (NSMutableArray *)executeQueryWithSqlstatement:(NSString *)sqlstatement queryModel:(Class)kClass{
    
    FMDatabase *database = [FIDFMDBManager createSqliteWithName:kRongGaoDataBaseName];
    
    NSMutableArray *params = [NSMutableArray array];
    if ([database open]) {
        
        FMResultSet *resultSet = [database executeQuery:sqlstatement];
        while ([resultSet next]) {
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            for (NSString *propertyname in [kClass propertyOfSelf]) {
                
                [param setObject:[resultSet stringForColumn:propertyname] forKey:propertyname];
                
            }
            [params addObject:[kClass yy_modelWithJSON:param]];
        }
        
    }
    [database close];
    
    return params ;
    
}

+ (void)updateDoorSystemModelInDatabase:(MyCamera *)dsModel{
    
    
    NSArray *deviceArray = [FIDFMDBManager executeQueryWithSqlstatement:[NSString stringWithFormat:@"select *from `%@`  where uid = '%@' ",kRongGaoCameraTableName,dsModel.uid] queryModel:object_getClass(dsModel)];
    if (deviceArray.count != 0) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE `%@` SET channel = '%@',dev_nickname = '%@', dev_name = '%@', dev_pwd = '%@' , view_acc = '%@',  view_pwd = '%@' ,where uid = '%@'",kRongGaoCameraTableName,[NSNumber numberWithInt:0],dsModel.nickname,dsModel.nickname,dsModel.viewPwd,dsModel.viewAcc,dsModel.viewPwd,dsModel.uid];
        
        
        BOOL isSuccess = [FIDFMDBManager executeUpdateWithSqlstatement:sql];
        if (isSuccess) {
            NSLog(@"更新成功");
        }else{
            NSLog(@"更新失败");
        }
    }else{
        
        [FIDFMDBManager executeUpdateWithSqlstatement:[NSString stringWithFormat:@"insert into `%@` values ('%@','%@','%@','%@','%@','%@','%@')",kRongGaoCameraTableName,[NSNumber numberWithInt:0],dsModel.nickname,dsModel.nickname,dsModel.viewPwd,dsModel.viewAcc,dsModel.viewPwd,dsModel.uid]];
        
    }
}

@end
